package cn.team11.smarteye.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import cn.team11.sds.dayi.R;

import org.apache.commons.lang3.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.team11.smarteye.activity.DeviceConfigActivity;
import cn.team11.smarteye.base.BaseFragment;
import cn.team11.smarteye.base.BasePresenter;
import cn.team11.smarteye.misc.SConstants;
import cn.team11.smarteye.utils.AppLogger;
import cn.team11.smarteye.utils.ToastUtil;

/**
 * Created by Yang on 2017/10/28.
 */

public class ConfigNetworkFragment extends BaseFragment {


    @BindView(R.id.network_name)
    TextView mNetworkName;
    @BindView(R.id.network_pwd)
    EditText mNetworkPwd;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_view_confirm_network, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onAttach(Context context) {
        mFragmentListener = (FragmentListener) getActivity();
        mFragmentListener.setTitle(getResources().getString(R.string.confirm_network));
        super.onAttach(context);

    }

    public static ConfigNetworkFragment newInstance(Bundle bundle) {
        ConfigNetworkFragment fragment = new ConfigNetworkFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        AppLogger.i("onViewCreated");
        init();
    }


    @Override
    public void onResume() {
        super.onResume();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    @Override
    public void init() {

        mNetworkName.setText(((DeviceConfigActivity)getActivity()).getCurrentNetworkName());
    }

    @OnClick(R.id.btn_net_confirm)
    public void confirm_network(View view){
        String ssid = mNetworkName.getText().toString().trim();
        String pwd = mNetworkPwd.getText().toString().trim();
        if (StringUtils.isEmpty(ssid) || StringUtils.isEmpty(pwd)){
            showToast(ToastUtil.NEGATIVE_TOAST, getString(R.string.invalid_network));
        } else {
            Bundle b = new Bundle();
            b.putString(SConstants.WIFI_NAME, ssid);
            b.putString(SConstants.WIFI_PWD, pwd);
            getActivity().getSupportFragmentManager().beginTransaction()
                    .hide(this)
                    .add(R.id.bind_fragment_content, QrcodeNetworkFragment.newInstance(b))
                    .addToBackStack(QrcodeNetworkFragment.class.getSimpleName())
                    .commitAllowingStateLoss();
        }
    }

    @Override
    public void setPresenter(BasePresenter presenter) {

    }
}
